import { NoticeService } from './../../providers/notice-service';
import { GroupDetailPage } from './../group-detail/group-detail';
import { UrlService } from './../../providers/url-service';
import { Component } from '@angular/core';
import { NavController, LoadingController, AlertController, ToastController } from 'ionic-angular';
import { UserInfo } from '../../global/ConstUrl';
import { UserDetialChallengePage } from '../user/user-detial-challenge/user-detial-challenge';
import { WythetanBasePage } from '../base/wythetan.page'

/**
 * 圈子
 */

@Component({
  selector: 'page-contact',
  templateUrl: 'contact.html'
})
export class ContactPage extends WythetanBasePage {

  indexPage = 1; //页数
  userID = UserInfo().UserID;
  group: boolean = true;//小组
  userDTlist_select: boolean = false;//动态
  haoyou: boolean = false;//战友

  constructor(
    public navCtrl: NavController,
    public noticeService: NoticeService,
    public urlService: UrlService,
    public loadingCtrl: LoadingController,
    public alertCtrl: AlertController,
    public toastCtrl: ToastController
  ) {
    super();
  }

  ionViewDidLoad() {
    this.loadData();
  }

  navSelected(key) {
    switch (key) {
      case "group":
        this.group = true;
        this.userDTlist_select = !this.group;
        this.haoyou = !this.group;
        break;
      case "userDTlist_select":
        this.userDTlist_select = true;
        this.group = !this.userDTlist_select;
        this.haoyou = !this.userDTlist_select;
        break;
      case "haoyou":
        this.haoyou = true;
        this.group = !this.haoyou;
        this.userDTlist_select = !this.haoyou;
        break;
    }
    this.loadData();
  }

  /**
   * 下拉刷新
   * @param refresher
   */
  doRefresh(refresher) {
    this.indexPage = 1;
    this.loadData(function() {
      refresher.complete();
    });
  }

  loadData(callBack = null) {
    if (this.group) {
      this.GetGroupRequest(this.indexPage, function() {
        callBack && callBack();
      });
    } else if (this.userDTlist_select) {
      this.GetGroupDynamicList(this.indexPage, function() {
        callBack && callBack();
      });
    } else {
      this.GetMyFrendy(this.indexPage, function() {
        callBack && callBack();
      });
    }
  }

	/**
	 * 加入小组
	 * @param pares 
	 */
  addGroup(pares) {
    let data = {
      ID: null,
      Creator: null,
      ApplyReason: null,
      type: false
    };
    if (pares.IsAgreeJoin != 1) {
      data.ApplyReason = "自动加入";
      data.type = true;
    } else {
      this.noticeService.presentPrompt("提示", "请填写您的申请原因", msg => {
        data.ApplyReason = msg.msg;
      });
    }
    data.ID = pares.ID;
    data.Creator = pares.Creator;
    this.addGroupServe(data);
  }

  addGroupServe(data) {
    this.urlService
      .GroupApplyFor(data.ID, data.Creator, data.ApplyReason)
      .then(data => {
        //console.log(data);
        if (data.Code == 0) {
          if (data.type == 1) {

            this.navCtrl.push(GroupDetailPage, { "pares": data });
          }
          if (data.Msg != undefined) {
            this.noticeService.showNoticeByToast(1, data.Msg);
          }
        } else {
          if (data.Msg != undefined) {
            this.noticeService.showNoticeByToast(1, data.Msg);
          }
        }
      })
      .catch((error) => {
        this.noticeService.showNoticeByToast(1, error.Msg);
      });
  }

  yaoqinglist = [];//待加入小组
  grouplist = [];//我的兴趣组
  /**
   * 获取小组
   * @param indexPage 
   */
  GetGroupRequest(indexPage, callBack = null) {
    this.loadingStart();
    this.urlService.GetGroupRequest(indexPage)
      .then(msg => {
        this.loadingEnd();
        //我的兴趣小组
        this.grouplist = msg.Data[0].grouplist.Items
        //热门达人
        this.yaoqinglist = msg.Data[0].invitationlist.Items;
        callBack && callBack();
      }).catch(err => {
        this.toast(err);
      }) 
  }

  userDTlist = [];//动态
  /**
   * 获取动态
   * @param {any} indexPage 
   * @memberof ContactPage
   */
  GetGroupDynamicList(indexPage, callBack = null) {
    this.loadingStart();
    this.urlService.GetGroupDynamicList(this.userID, indexPage)
      .then(msg => {
        this.loadingEnd();
        this.userDTlist = msg.Data.Items;
        callBack && callBack();
      }).catch(err => {
        this.toast(err);
      })
  }

  friendlist = [];//好友
  /**
   * 获取好友
   * @param indexPage
   */
  GetMyFrendy(indexPage, callBack = null) {
    this.loadingStart();
    this.urlService.GetMyFrendy(indexPage)
      .then(msg => {
        this.loadingEnd();
        this.friendlist = msg.Data.Items;
        callBack && callBack();
      }).catch(err => {
        this.toast(err);
      })
  }

  Btn_addf(item) {
    this.urlService.InsertRelationshipType(item.IsEachOther, item.ID)
      .then(data => {
        if (data.Code == 0) {
          if (item.IsEachOther == 0) {
            item.IsEachOther = 1;
          } else {
            item.IsEachOther = 0;
          }
        }
      }).catch(err => {
        this.toast(err);
      });
  }

  /**
   * 点赞
   */
  dianzan(item) {


  }

  // action

  /**
   * 
   * @param key 1、添加战友 2、创建
   */
  add(key) {
    alert(key);
    switch (key) {
      case 1://添加战友

        break;
      case 2://创建

        break;
      case 3://打开个人详情

        break;
    }
  }

	/**
	 * 打开小组详情
	 * @param pares 
	 */
  openGroupListDetial(pares) {
    this.navCtrl.push(GroupDetailPage, { "params": pares });
  }
  /**
   * 打开个人详情
   */
  openUserDetial(item) {
    this.navCtrl.push(UserDetialChallengePage, { "userID": item.ID });
  }
  openDy_detial() {

  }


}
